package com.winit.dataimport;

import com.alibaba.fastjson.JSONObject;
import com.winit.WebClientApplication;
import com.winit.commons.util.JSONFileUtil;
import com.winit.commons.util.NumberUtil;
import com.winit.commons.util.ObjectToString;
import com.winit.commons.util.SeqGenerate;
import com.winit.feature.service.IFeatureService;
import com.winit.menu.model.MatrixMenu;
import com.winit.menu.repository.MatrixMenuOperations;
import com.winit.menu.repository.MatrixMenuRepository;
import com.winit.page.model.Page;
import com.winit.page.repository.PageRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = WebClientApplication.class)
public class TomMenu {

    private static String zhString = null;
    private static String model = null;
    @Autowired
    MatrixMenuRepository menuRepository;
    @Autowired
    MatrixMenuOperations menuOperations;
    @Autowired
    PageRepository pageRepository;
    @Autowired
    private IFeatureService featureService;

    private List<MatrixMenu> menuList = new ArrayList<>();
    private List<Page> pageList = new ArrayList<>();

    @Test
    public void Test1() throws Exception {
        zhString = JSONFileUtil.getFileString("/Users/wangjian/Downloads/zh-cn.php");
        MatrixMenu rootMenu = getRootMenu();
        List<String> rootChild = new ArrayList<>();
        String[] menustr = new String[]{"OMS", "TMS", "WMS", "PMS", "IMS", "UMS", "Customer", "MMS", "EM", "UPS", "SMS", "Jinxuan", "HUB", "EXC", "Insight", "GFS", "Preferential"};
//        String[] menustr = new String[]{"OMS"};
        String filePath = "/Users/wangjian/Downloads/FeHelper-20180417103533.json";
        JSONObject json = JSONFileUtil.getFileJsonForBean(filePath, JSONObject.class);
        for (int i = 0; i < menustr.length; i++) {
            String key = menustr[i];
            model = key;
            System.out.println("=========================" + key + "=========================");
            MatrixMenu menu = getLevelOneMenu(json.getJSONObject(key), i + 1, rootMenu.getId());
            rootChild.add(menu.getId());
            List<String> list = new ArrayList<>();
            //处理子菜单
            dealSubMenu(json.getJSONObject(key), menu.getMenuNum(), 1, list, menu);
            menu.setChildren(list);
            saveMenu(menu);
        }
        rootMenu.setChildren(rootChild);
        saveMenu(rootMenu);
        //保存menu和page
//        menuRepository.save(menuList);
//        for (Page page : pageList) {
//            pageRepository.save(page);
//            featureService.createPrototypeForPage(page);
//        }
    }

    private MatrixMenu getRootMenu() {
        MatrixMenu menu = new MatrixMenu();
        menu.setId(SeqGenerate.createId("MENU"));
        menu.setMenu_name("TOM");
        menu.setReq_id("-1");
        menu.setSys("TOM");
        menu.setLevel("0");
        menu.setIs_del("1");
        menu.setStatus("ONLINE");
        Map<String, String> menu_data = new HashMap<>();

        menu_data.put("name", "TOM");
        menu_data.put("routeId", " ");
        menu.setMenu_data(menu_data);
        menu.setMenuNum("ROOT-TOM");
        menu.setParent_id("0");
        return menu;
    }

    private MatrixMenu getLevelOneMenu(JSONObject json, Integer index, String parentId) throws Exception {
        MatrixMenu menu = new MatrixMenu();
        menu.setId(SeqGenerate.createId("MENU"));
        String menuName = getMenuName(json.getString("label"));

        menu.setMenu_name(menuName);
        menu.setReq_id("-1");
        menu.setSys("TOM");
        menu.setLevel("1");
        menu.setIs_del("1");
        menu.setStatus("ONLINE");
        Map<String, String> menu_data = new HashMap<>();

        menu_data.put("name", menuName);
        menu_data.put("routeId", " ");
        menu.setMenu_data(menu_data);
        menu.setParent_id(parentId);
        menu.setMenuNum(NumberUtil.createNumber(NumberUtil.SysEnum.TOM, "ROOT-TOM", NumberUtil.TypeEnum.MENU, index));
        return menu;
    }

    private MatrixMenu getSubMenu(JSONObject json, String parent, Integer level, Integer index, String parentId) throws Exception {
        MatrixMenu menu = new MatrixMenu();
        menu.setId(SeqGenerate.createId("MENU"));
        String menuName = getMenuName(json.getString("name"));
        menu.setMenu_name(menuName);
        menu.setReq_id("-1");
        menu.setSys("TOM");
        menu.setLevel(String.valueOf(level + 1));
        menu.setIs_del("1");
        menu.setStatus("ONLINE");
        Map<String, String> menu_data = new HashMap<>();

        menu_data.put("name", menuName);
        menu_data.put("routeId", " ");
        menu.setMenu_data(menu_data);
        menu.setParent_id(parentId);
        menu.setMenuNum(NumberUtil.createNumber(NumberUtil.SysEnum.TOM, parent, NumberUtil.TypeEnum.MENU, index));
        return menu;
    }

    private void dealSubMenu(JSONObject json, String parent, Integer parentLevel, List<String> children, MatrixMenu parentMenu) throws Exception {
        if (!json.containsKey("subMenu") || json.getJSONArray("subMenu").size() == 0) {
            //最下级菜单添加一个同名的page
            Page page = getPage(parentMenu.getId(), parentMenu.getMenu_name(), parentMenu.getMenuNum());
            children.add(page.getId());
            savePage(page);
            return;
        }
        for (int i = 0; i < json.getJSONArray("subMenu").size(); i++) {
            JSONObject object = json.getJSONArray("subMenu").getJSONObject(i);
            MatrixMenu menu = getSubMenu(object, parent, parentLevel, i + 1, parentMenu.getId());
            children.add(menu.getId());
            List<String> list = new ArrayList<>();
            dealSubMenu(object, menu.getMenuNum(), parentLevel + 1, list, menu);
            menu.setChildren(list);
            saveMenu(menu);
        }
    }

    private String getMenuName(String label) {
        if (!zhString.matches("([\\S\\s]*)(['|\"])(" + label + ")(['|\"])(\\s*)(=>)(\\s*)(['|\"])(\\S*)(['|\"])(\\s*)(,)([\\S\\s]*)")) {
            getMenuNameFromModel(label);
            return label;
        }

        String result = zhString.replaceAll("([\\S\\s]*)(['|\"])(" + label + ")(['|\"])(\\s*)(=>)(\\s*)(['|\"])(\\S*)(['|\"])(\\s*)(,)([\\S\\s]*)", "$9");
        return result;
    }

    private Page getPage(String menuId, String label, String parent) throws Exception {
        Page page = new Page();
        page.setId(SeqGenerate.createId("MP"));
        page.setFileName(NumberUtil.createNumber(NumberUtil.SysEnum.TOM, parent, NumberUtil.TypeEnum.PAGE, 1));
        page.setMenuId(menuId);
        page.setTitle(label);
        return page;
    }

    private void saveMenu(MatrixMenu menu) {
//        System.out.println(ObjectToString.getString(menu));
//        menuList.add(menu);
    }

    private void savePage(Page page) {
//        System.out.println(ObjectToString.getString(page));
//        pageList.add(page);
    }

    private void getMenuNameFromModel(String label) {
        String string = JSONFileUtil.getFileString("/Users/wangjian/Documents/winit/winit2.0/tom/Apps/" + model + "/Lang/zh-cn.php");
        String result = null;

        if (zhString.matches("([\\S\\s]*)(['|\"])(" + label + ")(['|\"])(\\s*)(=>)(\\s*)(['|\"])([\\S\\s]*?)(['|\"])(\\s*)(,)([\\S\\s]*)")) {
            result = zhString.replaceAll("([\\S\\s]*)(['|\"])(" + label + ")(['|\"])(\\s*)(=>)(\\s*)(['|\"])([\\S\\s]*?)(['|\"])(\\s*)(,)([\\S\\s]*)", "$9");
            updateDb(label,result);
            return;
        }

        if (!string.matches("([\\S\\s]*)(['|\"])(" + label + ")(['|\"])(\\s*)(=>)(\\s*)(['|\"])([\\S\\s]*?)(['|\"])(\\s*)(,)([\\S\\s]*)")) {
            System.out.println("..................."+label);
            return;
        }
        result = string.replaceAll("([\\S\\s]*)(['|\"])(" + label + ")(['|\"])(\\s*)(=>)(\\s*)(['|\"])([\\S\\s]*?)(['|\"])(\\s*)(,)([\\S\\s]*)", "$9");
        updateDb(label,result);
    }

    private void updateDb(String label,String name){
        System.out.println(label+"===="+name);
        List<MatrixMenu> menus = menuRepository.findByMenuName(label);
        for (MatrixMenu menu:menus){
            menu.setMenu_name(name);
            menu.getMenu_data().put("name",name);
            menuRepository.save(menu);
        }
        List<Page> pages = pageRepository.findByTitle(label);
        for (Page page:pages){
            page.setTitle(name);
            pageRepository.save(page);
        }
    }



//    public static void main(String[] args) throws Exception {
//        Test1();
//    }
}
